﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Nuclex.Game.States;
using Nuclex.UserInterface.Controls.Desktop;
using Nuclex.UserInterface.Visuals.Flat;
using Nuclex.UserInterface;
using Nuclex.Input;
using Nuclex.Graphics.Debugging;
using Nuclex.Graphics.Batching;
using System.Diagnostics;

namespace WarPigs
{
    class MainMenuGameState : DrawableGameState
    {
        //Drawable Game State variables
        GraphicsDeviceManager graphics;
        GraphicsDevice device;
        SpriteBatch spriteBatch;
        ContentManager Content;
        private GameStateManager gameStateManager;
        WarPigs game;

        //GUI
        private GuiManager guiManager;
        private InputManager inputManager;
        Screen mainScreen;
        ButtonControl newGameButton;
        ButtonControl multiplayerGameButton;
        ButtonControl quitButton;
        ButtonControl optionButton;
        SinglePlayerDialog spDialog;

        public string test = " ";

        //Fonts
        private SpriteFont arial24sprite;

        public pmcData data;

        public MainMenuGameState(GraphicsDeviceManager passedGraphics, GraphicsDevice passedDevice, ContentManager passedContent, SpriteBatch passedBatch, GuiManager passedManager, InputManager passedInput, GameStateManager passedStateManager, Game passedGame, pmcData passedData)
        {
            graphics = passedGraphics;
            device = passedDevice;
            Content = passedContent;
            spriteBatch = passedBatch;
            guiManager = passedManager;
            inputManager = passedInput;
            gameStateManager = passedStateManager;
            game = (WarPigs)passedGame;
            data = passedData;

            Viewport viewport = device.Viewport;
            mainScreen = new Screen(viewport.Width, viewport.Height);
            guiManager.Screen = mainScreen;




            //Font loader
            arial24sprite = Content.Load<SpriteFont>(@"ArialFont");                
        }



        protected override void OnEntered()
        {
            //Create Controls
            // Each screen has a 'desktop' control. This invisible control by default
            // stretches across the whole screen (all controls are positioned using both
            // a percentual position/size and absolute position/size). We change this to
            // prevent GUI or HUD elements from appearing outside the title-safe area.
            mainScreen.Desktop.Bounds = new UniRectangle(
              new UniScalar(0.1f, 0.0f), new UniScalar(0.1f, 0.0f), // x and y
              new UniScalar(0.8f, 0.0f), new UniScalar(0.8f, 0.0f) // width and height
            );
            // control, which means it will become visible and interactive.
            //mainScreen.Desktop.Children.Add(new SinglePlayerDialog());
            createDesktopControls(mainScreen);

            base.OnEntered();
        }

        protected override void OnLeaving()
        {
            //Destroy Controls
            base.OnLeaving();
        }

        protected override void OnPause()
        {
            
            base.OnPause();
        }

        protected override void OnResume()
        {


            base.OnResume();
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            spriteBatch.Begin();
            CreateMenuText();
            spriteBatch.End();
        }

        //Creates the New, MP, and Quit buttons.
        private void createDesktopControls(Screen mainScreen)
        {

            // Button to open the "New Game" dialog
            newGameButton = new ButtonControl();
            newGameButton.Text = "Single Player Game";
            newGameButton.Bounds = new UniRectangle(
              new UniScalar(1.0f,-110.0f), new UniScalar(1.0f, -120.0f), 140, 32
            );
            newGameButton.Pressed += new EventHandler(newGameButton_Pressed);
            mainScreen.Desktop.Children.Add(newGameButton);

            // Button to open a "Multiplayer" dialog
            multiplayerGameButton = new ButtonControl();
            multiplayerGameButton.Text = "Multi Player Game";
            multiplayerGameButton.Bounds = new UniRectangle(
              new UniScalar(1.0f, -110.0f), new UniScalar(1.0f, -75.0f), 140, 32
            );
            multiplayerGameButton.Pressed += new EventHandler(multiplayerGameButton_Pressed);
            mainScreen.Desktop.Children.Add(multiplayerGameButton);

            // Button through which the user can set application options
            optionButton = new ButtonControl();
            optionButton.Text = "Options";
            optionButton.Bounds = new UniRectangle(
              new UniScalar(1.0f, -80.0f), new UniScalar(1.0f, -32.0f), 80, 32
            );
            //optionButton.Pressed += new EventHandler(optionButton_Pressed);
            mainScreen.Desktop.Children.Add(optionButton);

            // Button through which the user can quit the application
            quitButton = new ButtonControl();
            quitButton.Text = "Quit";
            quitButton.Bounds = new UniRectangle(
              new UniScalar(1.0f, -80.0f), new UniScalar(1.0f, 10.0f), 80, 32
            );
            quitButton.Pressed += new EventHandler(quitButton_Pressed);
            mainScreen.Desktop.Children.Add(quitButton);

            //Single player options
            float dialogWidth = (graphics.PreferredBackBufferWidth / 1.1f);
            float dialogHeight = (graphics.PreferredBackBufferHeight / 1.1f);
            spDialog = new SinglePlayerDialog(this, dialogWidth, dialogHeight);
        }

        //Button Clicks
        void newGameButton_Pressed(object sender, EventArgs e)
        {
            mainScreen.Desktop.Children.Add(spDialog);
            spDialog.BringToFront();
            spDialog.EnableDragging = false;
        }
        void multiplayerGameButton_Pressed(object sender, EventArgs e)
        {

        }

        void optionButton_Pressed(object sender, EventArgs e)
        {

        }

        void quitButton_Pressed(object sender, EventArgs e)
        {
            game.Exit();
        }
        

        //Starts the game and removes the mm ui.
        public void newGameStart()
        {
            mainScreen.Desktop.Children.Remove(newGameButton);
            mainScreen.Desktop.Children.Remove(multiplayerGameButton);
            mainScreen.Desktop.Children.Remove(quitButton);
            mainScreen.Desktop.Children.Remove(optionButton);
            
            //gameStateManager.;
            game.ChangeState("inGameGameState");
        }

        public void CreateMenuText()
        {
            if (game.data.pmcName != null)
                test = System.String.Format("Welcome, {0}", game.data.pmcName);
            this.spriteBatch.DrawString(
            this.arial24sprite,
            "War Pigs\nTech Demo v0.2\nCreated by Patrick Gurney\nCopyright 2011",
            new Vector2(60.0f, 60.0f),
            new Color(255, 255, 255, 255)
            );
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    } 
}